﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Prop
{
    class Shop
    {
        private int shopNum;
        private List<Item> items;
        private List<Order> orders;
        private static long orderNo;


        DataHelper dh = new DataHelper();
        

        public Shop(int shopNum)
        {
            this.shopNum = shopNum;
            orders = new List<Order>();

           items =  dh.loadAllProducts();
        }
        public int ShopNum
        {
        get{ return shopNum;}
        }
        public List<Item> getAllItems()
        {
            return items;
        }
     
        public List<Item> showItems()// shows all the items in the shop.
        {
            List<Item> v;
            v = new List<Item>();
            return v;//todo
        }
        public Item getItem(int prodId)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].Id == prodId)
                {
                    return items[i];
                }
            }
            return null;
        }
        public void addOrder(ShopVisitor c, double price, List<int> prodIds)
        {
            
            Order o = new Order(orderNo, DateTime.Now, c, price);
            orderNo++;
            orders.Add(o);
            o.processOrder(shopNum);
            for (int i = 0; i < prodIds.Count; i++)
            {
                Item r = getItem(prodIds[i]);
                for (int j = 0; j < r.NumOrdered; j++)
                {
                    o.addOrderLine(prodIds[i]);
                }

            }
            dh.updateBalance(c.VisitorId,c.Balance);
            dh.updateMoneySpent(c.VisitorId, price);
            for (int j = 0; j < items.Count; j++)
            {
                dh.updateNoInStock(items[j].Id, items[j].NumInStock);
            }
        }
        public void plusStock(int prodId)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].Id == prodId)
                {
                    items[i].NumInStock++;
                }
            }
        }
    }
}
